package org.snail.chat.service.chatchain;

import lombok.NonNull;
import lombok.extern.log4j.Log4j;
import org.apache.log4j.Logger;
import org.snail.chat.service.chatchain.chain.ChatCreateOrUpdateHandler;
import org.snail.chat.service.chatchain.dao.ChatCreateOrUpdateDAO;
import org.snail.chat.service.chatchain.regular.HandlerManger;
import org.snail.dept.bean.TreeNode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * chat执行链
 */
@Log4j
public class ChatCreateOrUpdateChain {

    private static final Logger LOGGER =  Logger.getLogger(ChatCreateOrUpdateChain.class);

    public static Map<String, String> lvOwnerMap = new HashMap<>();   //这个是对应部门层级的
    public static Map<String, String> deptIdOwnerMap = new HashMap<>();   //这个是对应部门id的 特殊配置的

    /**
     * 执行节点处理
     * @param treeNode 部门树
     * @param chatCreateOrUpdateDAOMap 这个应该是生成的规则Map
     */
    public static void executeChatCreateOrUpdateChain(@NonNull TreeNode treeNode , Map<String, ChatCreateOrUpdateDAO> chatCreateOrUpdateDAOMap, int loopCount) throws Exception {

        if(null!=treeNode && loopCount>=0) {
            //层数减少
            loopCount--;

            //根据ID获取规则，如果没有,则根据LV获取
            String id = treeNode.getId();
            ChatCreateOrUpdateHandler chatCreateOrUpdateHandler = HandlerManger.getChatCreateOrUpdateHandler(id);
            log.info("chatCreateOrUpdateHandler : " + chatCreateOrUpdateHandler);
            if(null==chatCreateOrUpdateHandler) {
                chatCreateOrUpdateHandler = HandlerManger.getChatCreateOrUpdateHandler(treeNode.getLv());
            }

            //执行当前节点逻辑
            if(null!=chatCreateOrUpdateHandler) {
                chatCreateOrUpdateHandler.doChatCreateOrUpdateHandler(treeNode, chatCreateOrUpdateDAOMap);
            }

            //执行下层逻辑
            List<TreeNode> childTreeNodeList = treeNode.getChildren();
            if(null!=childTreeNodeList && !childTreeNodeList.isEmpty()) {
                for (TreeNode oneChildTreeNode :  childTreeNodeList) {
                    executeChatCreateOrUpdateChain(oneChildTreeNode, chatCreateOrUpdateDAOMap, loopCount);
                }
            }
        }
    }

}
